package service;

import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import bean.Delivery;
import mapper.DeliveryMapper;
import mapper.GoodsMapper;
import mapper.Mapper;
import mapper.TaskMapper;

public class DeliveryInfoService extends Mapper<DeliveryMapper> {
	private long total;

	public String getAllDeliveryJson(Integer userid, String spage, String startTime, String endTime, String deliveryno, String transportStatus, String applyStatus, String lackStatus, String addressee, String payno) {
//		int page = Integer.parseInt(spage);
//		Page<Object> pageHelper = PageHelper.startPage(page, 5);
		Map<String, Object> map = new HashMap<>();
		map.put("userid", userid);
		if (startTime != null && !startTime.isEmpty())
			map.put("startTime", Date.valueOf(startTime + " 00:00:00"));
		if (endTime != null && !endTime.isEmpty())
			map.put("endTime", Date.valueOf(endTime + " 00:00:00"));
		map.put("deliveryno", deliveryno);
		map.put("transportStatus", transportStatus);
		map.put("applyStatus", applyStatus);
		map.put("lackStatus", lackStatus);
		map.put("addressee", addressee);
		map.put("payno", payno);
		List<Delivery> list = getMapper().findAll(map);
//		total = pageHelper.getTotal();

		return deliveryListToJson(list);
	}

	private String deliveryListToJson(List<Delivery> deliveryList) {
		TaskMapper mapper = new TaskInfoService().getMapper();
		GoodsMapper goodsMapper = new GoodsInfoService().getMapper();
		Gson gson = new Gson();
		JsonElement jsonTree = gson.toJsonTree(deliveryList);
		JsonArray jsonArray = jsonTree.getAsJsonArray();
		Iterator<JsonElement> iterator = jsonArray.iterator();
		while (iterator.hasNext()) {
			JsonObject jsonObject = iterator.next().getAsJsonObject();
			String deliveryNo = jsonObject.get("no").getAsString();
			// 添加任务信息
			JsonElement taskJsonTree = gson.toJsonTree(mapper.findByDeliveryNo(deliveryNo));
			JsonArray taskJsonArray = taskJsonTree.getAsJsonArray();
			
			Iterator<JsonElement> taskIterator = taskJsonArray.iterator();
			while (taskIterator.hasNext()) {
				JsonObject taskJsonObject = taskIterator.next().getAsJsonObject();
				// 添加客户信息
//				JsonElement userJsonTree = gson.toJsonTree(userMapper.findById(jsonObject.get("user_id").getAsInt()));
//				jsonObject.add("user", userJsonTree);
				// 添加商品信息
				JsonElement goodsJsonTree = gson.toJsonTree(goodsMapper.findById(taskJsonObject.get("goods_id").getAsInt()));
				taskJsonObject.add("goods", goodsJsonTree);
			}
			jsonObject.add("tasks", taskJsonTree);
		}
		// 总数
		String totalJson = "{'total':" + total + "}";
		JsonObject totalObject = new JsonParser().parse(totalJson).getAsJsonObject();
		jsonArray.add(totalObject);
		return gson.toJson(jsonArray);
	}
}
